package com.wenhai.leetcode.search;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName _173二叉搜索树迭代器
 * @Description https://leetcode-cn.com/problems/binary-search-tree-iterator/
 * @Author 谢文海
 * @Date 2021/3/28 15:39
 * @Version 1.0
 **/
public class _173二叉搜索树迭代器 {
    private int idx;
    private List<Integer> arr;

    public _173二叉搜索树迭代器(TreeNode root) {
        idx = 0;
        arr = new ArrayList<>();
        inorderTraversal(root, arr);
    }


    public int next() {
        return arr.get(idx++);
    }

    public boolean hasNext() {
        return idx < arr.size();
    }

    private void inorderTraversal(TreeNode node, List<Integer> arr) {
        if (node == null) {
            return;
        }
        inorderTraversal(node.left, arr);
        arr.add(node.val);
        inorderTraversal(node.right, arr);
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

}
